package cn.hrlly.tao2shou.view.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.hrlly.tao2shou.R;
import cn.hrlly.tao2shou.adapter.SellerUnfinishOrderAdapter;
import cn.hrlly.tao2shou.domain.OrderList;
import cn.hrlly.tao2shou.domain.Orders;
import cn.hrlly.tao2shou.other.Config;
import cn.hrlly.tao2shou.presenter.OperateOrderPresenter;
import cn.hrlly.tao2shou.presenter.OperateOrderPresenterImpl;
import cn.hrlly.tao2shou.presenter.OrderListPresenter;
import cn.hrlly.tao2shou.presenter.OrderListPresenterImpl;
import cn.hrlly.tao2shou.utils.NetUtil;
import cn.hrlly.tao2shou.view.activity.OrderDetailsActivity;
import cn.hrlly.tao2shou.view.view_interface.OperateOrderView;
import cn.hrlly.tao2shou.view.view_interface.OrderListView;

public class SellerUnfinishOrderFragment extends Fragment implements OrderListView,OperateOrderView{

    @Bind(R.id.lvOrder)
    ListView lvOrder;
    public String orderId;
    private OrderListPresenter orderListPresenter;
    private OperateOrderPresenter operateOrderPresenter;
    private ArrayList<Orders> list;
    private SellerUnfinishOrderAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_seller_unfinish_order, container, false);
        ButterKnife.bind(this, rootView);
        init();
        clickListview();
        return rootView;
    }

    //初始化
    private void init() {
        orderId=null;
        list=new ArrayList<Orders>();
        orderListPresenter=new OrderListPresenterImpl(this,getActivity());
        operateOrderPresenter=new OperateOrderPresenterImpl(this,getActivity());
        if (NetUtil.ping(getActivity())){
            orderListPresenter.showNetOrderList();
        }else {
            orderListPresenter.showCacheOrderList();
        }
    }

    //listview点击事件
    public void clickListview(){
        lvOrder.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(getActivity(), OrderDetailsActivity.class);
                intent.putExtra(Config.key_order_id,list.get(position).getOrderId());
                startActivity(intent);
            }
        });
    }

    //以下是对operateOrderView的操作
    @Override
    public String getOrderId() {
        return orderId;
    }

    //以下是orderListView的操作
    @Override
    public int getOrderType() {
        return 2;
    }

    @Override
    public void showMessage(String errorMessage) {
        Toast.makeText(getActivity(),errorMessage,Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showMessage(int errorMessage) {
        Toast.makeText(getActivity(),errorMessage,Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showListView(OrderList orderList) {
        list.addAll(orderList.getOrders());
        adapter=new SellerUnfinishOrderAdapter(list,this,operateOrderPresenter,getActivity());
        lvOrder.setAdapter(adapter);
    }
}
